package com.glwan.fixedassetapp.ui.setting.activity;

import android.os.Build;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.glwan.fixedassetapp.R;
import com.glwan.fixedassetapp.helper.RFIDDeviceInterface;
import com.glwan.fixedassetapp.utils.CommonUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class RFIDSettingActivity extends AppCompatActivity {

    EditText ePower;

    Spinner spSession;

    List<String> spSessionList = new ArrayList<>();

    RFIDDeviceInterface rfidDeviceInterface;

    //
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragement_setting_rfid_activity);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        setTitle(R.string.RFIDSetting);

        //显示返回按钮
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setHomeButtonEnabled(true);
            actionBar.setDisplayHomeAsUpEnabled(true);
        }


        ePower = findViewById(R.id.ePower);

        spSession = findViewById(R.id.spSession);

        setSelectData(0, "S0", "S1", "S2", "S3");
        spSession.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, spSessionList));


        rfidDeviceInterface = RFIDDeviceInterface.getInstance(Build.BRAND, this);
        ePower.setText(String.valueOf(rfidDeviceInterface.getPower()));

        spSession.setSelection(rfidDeviceInterface.getSession(), true);

    }

    private void setSelectData(int type, String... strArray) {
        if (type == 0) {
            spSessionList.addAll(Arrays.asList(strArray));
        }
    }

    //返回主界面
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {

        if (rfidDeviceInterface.setPower(Integer.parseInt(ePower.getText().toString())) && rfidDeviceInterface.setSession(spSession.getSelectedItemPosition())) {
            CommonUtil.toastShow(this, getString(R.string.setSuccess), CommonUtil.SUCCESS, true);
        } else {
            CommonUtil.toastShow(this, getString(R.string.setFail), CommonUtil.ERROR, true);
        }

        if (item.getItemId() == android.R.id.home) {
            finish();
        }
        return super.onOptionsItemSelected(item);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();

        if (rfidDeviceInterface.setPower(Integer.parseInt(ePower.getText().toString())) && rfidDeviceInterface.setSession(spSession.getSelectedItemPosition())) {
            CommonUtil.toastShow(this, getString(R.string.setSuccess), CommonUtil.SUCCESS, true);
        } else {
            CommonUtil.toastShow(this, getString(R.string.setFail), CommonUtil.ERROR, true);
        }

    }


}
